package service.impl;

import mapper.TbItemCatMapper;
import org.springframework.stereotype.Service;
import pojo.EasyUiTreeNode;
import pojo.TbItemCat;
import pojo.TbItemCatExample;
import service.ItemCatService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 获取EasyUi-Tree返回商品类目数据
     *
     * @param parentId
     * @return
     */
    @Override
    public List<EasyUiTreeNode> getEasyUiTreeNode(long parentId) {
        //创建TbItemCatExample对象
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        //创建查询条件
        criteria.andParentIdEqualTo(parentId);
        //获取类目数据
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(tbItemCatExample);
        //创建返回的list对象,泛型为EasyUiTreeNode
        List<EasyUiTreeNode> easyUiTreeNodesList = new ArrayList<>();
        //遍历查询结果
        for (TbItemCat tbItemCat : tbItemCatList){
            //创建EasyUiTreeNode对象，插入对象值，最后放到easyUiTreeNodesList中
            EasyUiTreeNode treeNode = new EasyUiTreeNode();
            treeNode.setId(tbItemCat.getId());
            treeNode.setText(tbItemCat.getName());
            treeNode.setState(tbItemCat.getIsParent()?"closed":"open");
            easyUiTreeNodesList.add(treeNode);
        }
        return easyUiTreeNodesList;
    }
}
